/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comandaXM.vista.auxiliars;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import comandaXM.accions.AuxiliarsAct; 
import comandaXM.classes.manteniment.auxiliar.TipusIva;

import comandaXM.excepcions.ExceptionHibernate; 
import comandaXM.excepcions.ExceptionXML; 
import comandaXM.utils.JDialogXM1;
import comandaXM.utils.TDSLanguageUtils;
import comandaXM.utils.TableModel;

import java.util.ArrayList;
/**
 *
 * @author xmarti
 */
public abstract  class JDIves extends JDialogXM1  {    
    private List<TipusIva>tipusIves;
    private TipusIva tipusIva;
    private AuxiliarsAct accio;
    private JPAuxiliarDataIva jPData;
    public JDIves(){        
        super(null,true,"JdIva",TDSLanguageUtils.getMessage("jDialogIva.JDialog.caption"));        
        this.accio=new AuxiliarsAct (); 
        this.jPData= new JPAuxiliarDataIva() {
            @Override
            public void movePosition(int e) {
                incrementarJTableXM(e);
            }
        };
        this.jPData.addKeyCodeListener(jPData);
        super.getjPanelData().setViewportView(this.jPData);
        try {
            super.chargeTable();
        } catch (ExceptionHibernate ex) {
            Logger.getLogger(JDIves.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExceptionXML ex) {
            Logger.getLogger(JDIves.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }

    @Override
    public TableModel getModel(String textToSearch) throws ExceptionHibernate {
       TableModel dadesTaula = null;                
            this.tipusIves=(List<TipusIva>) this.accio.getTipusIva(textToSearch);
            String [] columns={"",
                          TDSLanguageUtils.getMessage("jDialogIva.JTable.codi.titol"),                               
                          TDSLanguageUtils.getMessage("jDialogIva.JTable.nom.titol"),
                          TDSLanguageUtils.getMessage("jDialogIva.JTable.valor.titol"),                          
                          TDSLanguageUtils.getMessage("jDialogIva.JTable.recarreg.titol"),                          
                          TDSLanguageUtils.getMessage("jDialogIva.JTable.estat.titol")                          
                          };    
            Object[][] dades=null;
            
           if (this.tipusIves.size()>0){
                dades=new Object[this.tipusIves.size()][columns.length];                          
                Integer i=0;            
                for (Iterator it = this.tipusIves.iterator(); it.hasNext();) {
                    TipusIva c = (TipusIva) it.next();                
                    dades[i][0]= c; 
                    if (c.getCodi() ==(null))dades[i][1]="-"; else dades[i][1]= c.getCodi();
                    if (c.getNom() ==(null))dades[i][2]="-"; else dades[i][2]= c.getNom();                    
                    dades[i][3]=c.getImpost();
                    dades[i][4]=c.getrEquivalencia();
                    dades[i][5]=c.isActiu(); 
                    i++;
                }     
            }                  
        return new TableModel(columns, dades);   
    }

    @Override    
    public void getData() throws ExceptionHibernate  {                        
        this.tipusIva.setNom(this.jPData.getNameAuxiliar());
        this.tipusIva.setCodi(this.jPData.getCode());
        this.tipusIva.setActiu(this.jPData.getEstat());
        try{
            this.tipusIva.setImpost(Integer.parseInt(this.jPData.getImpost()) );        
            this.tipusIva.setrEquivalencia(Double.parseDouble(this.jPData.getRecarrec()));
        }catch (NumberFormatException nFe){                        
            throw new ExceptionHibernate(ExceptionHibernate.ERR_PARSE_INTEGER);            
        }        
    }
    
    @Override
    public void setData() {
       
       if(super.isInsert()) this.tipusIva=new TipusIva();
       else this.tipusIva=(TipusIva) super.getObject();
       this.jPData.setNameAuxiliar(this.tipusIva.getNom());                
       this.jPData.setCode(this.tipusIva.getCodi()); 
       this.jPData.setImpost(String.valueOf(this.tipusIva.getImpost()));
       this.jPData.setCodeRecarrec(String.valueOf(this.tipusIva.getrEquivalencia()));
       this.jPData.setEstat(this.tipusIva.isActiu());
       this.jPData.setRequestFocus();
    }
    @Override
    public void saveData() throws ExceptionHibernate  {                
        TipusIva temp = new TipusIva(tipusIva.getId(),tipusIva.getCodi(),tipusIva.getNom(),tipusIva.getImpost(),tipusIva.getrEquivalencia(),tipusIva.isActiu()) ; 
        getData();        
        try{
            if (super.isInsert()){
                this.tipusIva.setId(this.accio.add(this.tipusIva));

            }else{
                this.accio.update(this.tipusIva);                
            } 
            actualitzar(); 
        }catch (ExceptionHibernate ex){
            tipusIva.setCodi(temp.getCodi());            
            tipusIva.setNom(temp.getNom());
            tipusIva.setImpost(temp.getImpost());
            tipusIva.setrEquivalencia(temp.getrEquivalencia());
            tipusIva.setActiu(temp.isActiu());
            setData(); 
            throw new ExceptionHibernate (ex.getMessage()); 
            //JOptionPane.showMessageDialog(null,ex.getMessage() ,TDSLanguageUtils.getMessage("save.data.ERR"),JOptionPane.ERROR_MESSAGE);
        }finally{
            temp=null;             
        }
        
    }
    @Override
    public boolean validation (){
        if ("".equals(jPData.getCode())){
            jPData.setErrorCode(TDSLanguageUtils.getMessage("jDialogIva.code.error"));
            return false;  
        }
        else if ("".equals(jPData.getImpost())){
            jPData.setErrorImpost(TDSLanguageUtils.getMessage("jDialogIva.impost.error"));
            return false; 
        }
        else if (Integer.parseInt(jPData.getImpost())<0){
            jPData.setErrorCode(TDSLanguageUtils.getMessage("jDialogIva.impost.positiu.error"));
            return false; 
        }      
        if ("".equals(jPData.getNameAuxiliar())){
            jPData.setErrorName(TDSLanguageUtils.getMessage("jDialogIva.name.error"));
            return false; 
        }
        return true; 
    }
    @Override
    public void removeData() throws ExceptionHibernate {
        this.accio.delete(this.tipusIva);
        super.setObject(new TipusIva());
        actualitzar(); 
    }

    @Override
    public boolean isSaved() {        
        //if (!this.isValid()) return true; 
        if (this.isInsert()) return true; 
        if(this.tipusIva==null) return true;
        if (!this.tipusIva.getNom().equals(this.jPData.getNameAuxiliar())) return false;                
        if (!this.tipusIva.getCodi().equals(this.jPData.getCode())) return false ; 
        if(this.tipusIva.isActiu()!= this.jPData.getEstat()) return false ;
        try{
            if(this.tipusIva.getImpost()!=Integer.parseInt(this.jPData.getImpost()) ) return false; 
            if(this.tipusIva.getrEquivalencia()!=Double.parseDouble(this.jPData.getRecarrec())) return false; 
        }catch(NumberFormatException nfe){
            return true ; 
        }
        
        return true;
    }       
    private void incrementarJTableXM(int e){
        super.incrementarPosRegistre(e);
    }
    public abstract void actualitzar(); 
}
